"""
API数据模型
"""
from typing import List, Optional, Dict, Any
from pydantic import BaseModel, Field
from datetime import datetime

class FileUploadResponse(BaseModel):
    """文件上传响应"""
    success: bool
    file_id: Optional[str] = None
    filename: Optional[str] = None
    message: Optional[str] = None
    error: Optional[str] = None

class FileInfo(BaseModel):
    """文件信息"""
    file_id: str
    filename: str
    file_size: int
    file_type: str
    status: str
    chunk_count: int
    vector_count: int
    upload_time: Optional[str] = None
    process_time: Optional[str] = None
    title: Optional[str] = None
    author: Optional[str] = None

class FileListResponse(BaseModel):
    """文件列表响应"""
    files: List[FileInfo]
    pagination: Dict[str, Any]

class FileDetailResponse(BaseModel):
    """文件详情响应"""
    success: bool
    file_detail: Optional[Dict[str, Any]] = None
    error: Optional[str] = None

class SearchRequest(BaseModel):
    """搜索请求"""
    query: str = Field(..., min_length=1, max_length=1000, description="搜索查询")
    top_k: Optional[int] = Field(10, ge=1, le=100, description="返回结果数量")
    filter_expr: Optional[str] = Field(None, description="过滤表达式")
    user_id: Optional[str] = Field(None, description="用户ID")
    session_id: Optional[str] = Field(None, description="会话ID")

class SearchResult(BaseModel):
    """搜索结果"""
    rank: int
    id: int
    score: float
    content: str
    file_id: str
    chunk_id: str
    chunk_index: int
    file_type: str
    filename: str
    metadata: Dict[str, Any]
    created_time: int

class SearchResponse(BaseModel):
    """搜索响应"""
    success: bool
    query: str
    results: List[SearchResult]
    total_found: int
    total_returned: int
    search_time: float
    similarity_threshold: float
    cached: Optional[bool] = False
    source: Optional[str] = "vector"  # 检索来源: vector, database, cache
    milvus_available: Optional[bool] = True  # Milvus是否可用
    error: Optional[str] = None

class BatchSearchRequest(BaseModel):
    """批量搜索请求"""
    queries: List[str] = Field(..., min_items=1, max_items=10)
    top_k: Optional[int] = Field(10, ge=1, le=100)

class BatchSearchResponse(BaseModel):
    """批量搜索响应"""
    results: List[Dict[str, Any]]

class SystemStatsResponse(BaseModel):
    """系统统计响应"""
    files: Dict[str, Any]
    vectors: Dict[str, Any]
    file_types: List[Dict[str, Any]]
    milvus: Dict[str, Any]

class SearchHistoryItem(BaseModel):
    """检索历史项"""
    search_id: str
    query: str
    query_type: str
    result_count: int
    search_time: float
    created_time: str
    user_id: Optional[str] = None
    session_id: Optional[str] = None

class SearchHistoryResponse(BaseModel):
    """检索历史响应"""
    history: List[SearchHistoryItem]

class SearchStatsResponse(BaseModel):
    """检索统计响应"""
    total_searches: int
    average_search_time: float
    popular_queries: List[Dict[str, Any]]

class HealthCheckResponse(BaseModel):
    """健康检查响应"""
    status: str
    timestamp: str
    version: str
    services: Dict[str, str]

class ErrorResponse(BaseModel):
    """错误响应"""
    success: bool = False
    error: str
    error_code: Optional[str] = None
    details: Optional[Dict[str, Any]] = None
